---
title: "DST migration analyses"
description: "When do tag 308 and 321 start/stop migrating?"
# author: "Lotte Pohl"
execute:
echo: false
include: true
output:
html_document:
df_print: paged
theme: readable
# toc_float: true
toc: true
# number_sections: false
# toc_depth: 2
date: 3.20.2023
editor_options:
chunk_output_type: console
knitr:
opts_chunk:
collapse: true
format:
html:
page-layout: article
code-tools: true
fig-width: 8
fig-height: 5
toc: true
image: image.PNG
draft: false
---
```{r libraries}
#| include: false
library (tidyverse)
library (dplyr)
library (plotly)
library (ggplot2)
library (sf)
library (suncalc)
library (dplyr)
library (lubridate)
library (pracma)
library (psdr)
library (StreamMetabolism)
```
```{r workspace}
#| include: false
# rm(list = ls())
dir_path <- "C:/Users/lotte.pohl/Documents/github_repos/ADST_Mustelus_asterias"
# source(paste0(dir_path, "/02_scripts/02_load_data/load_depth_temp_logs.R"))
# source(paste0(dir_path, "/02_scripts/04_analyses/FFT/calculate_fft_psd.R"))
# source(paste0(dir_path, "/02_scripts/05_plots_maps/plot_fft_periodogram.R"))
# source(paste0(dir_path, "/02_scripts/05_plots_maps/plot_dst_pressure_temp.R"))
# source(paste0(dir_path, "/02_scripts/05_plots_maps/plots_dst_pca_kmeans.R"))
source (paste0 (dir_path, "/02_scripts/05_plots_maps/plots_dst_summary.R" ))
source (paste0 (dir_path, "/02_scripts/05_plots_maps/base_map.R" ))
source (paste0 (dir_path, "/02_scripts/02_load_data/load_dst_geolocation_output.R" ))
```
<!-- :::{.column-page-inset} -->
:::{.column-body}
> Research Question: can migration and residency behaviour be inferred from the depth log?
# vertical space use
<!-- **Question**: when did the sharks start to migrate? -->
## Depth median, min and max
:::{.panel-tabset}
### female
```{r}
#| layout-ncol: 1
##| fig-height: 4
#| column: page
#| fig-cap: Ribbon plot showing the daily median, minimum and maximum depth of tag 308.
#| - ""
p_308_depth_median_range_change_ribbon %>% plotly:: ggplotly () %>%
plotly:: layout (legend = list (orientation = "h" ,
x = 0.5 , y = - 0.2 ,
xanchor = "center" , yanchor = "top" ))
```
* There are peaks in the change of daily median in September, December (this is influenced by the non-accurate depth measurements, though), March and May.
* There seems to be no clearly visible pattern in daily vertical migration, comparing between potentially migrating and residential periods.
* These months I will have a further look into.
<!-- i.e. daily vertical migration behaviour for these months. weekly depth distribution -->
### male
```{r}
#| layout-ncol: 1
##| fig-height: 4
#| column: page
#| fig-cap: Ribbon plot showing the daily median, minimum and maximum depth of tag 321.
#| - ""
p_321_depth_median_range_change_ribbon %>% plotly:: ggplotly () %>%
layout (legend = list (orientation = "h" ,
x = 0.5 , y = - 0.2 ,
xanchor = "center" , yanchor = "top" ))
```
* Here we see peaks in the change of daily median in October 2018, May 2019, August 2019 and October 2019.
* Both in October 2018 and 2019, we see reverse Diel Vertical Migration.
:::
*Note: a rolling mean over 3 days was applied to smooth the data.*
## Daily Vertical Migration
* Diel Vertical Migration (DVM): Depth during day significantly deeper than during night.
* reverse DVM (rDVM): Depth during night significantly deeper than during day.
* no DVM (nDVM): No significant difference between depth during night and day.
:::{.panel-tabset}
### female
<!-- To Do: get weeks of where 308/321 potentially started migrating and then plot daily DVM -->
```{r}
#| layout-ncol: 1
##| fig-height: 4
#| column: page
#| fig-cap: Ribbon plot showing the daily median, minimum and maximum depth of tag 308.
#| - ""
p_tag308_DVM_weekly %>% plotly:: ggplotly () %>%
layout (legend = list (orientation = "h" ,
x = 0.5 , y = - 0.5 ,
xanchor = "center" , yanchor = "top" ))
```
### male
```{r}
#| layout-ncol: 1
##| fig-height: 4
#| column: page
#| fig-cap: Ribbon plot showing the daily median, minimum and maximum depth of tag 321.
#| - ""
p_tag321_DVM_weekly %>% plotly:: ggplotly () %>%
layout (legend = list (orientation = "h" ,
x = 0.5 , y = - 0.5 ,
xanchor = "center" , yanchor = "top" ))
```
:::
## Vertical speed
Vertical speed = the absolute difference between two subsequent measurements divided by the number of samples taken per minute (Units = m/min).
:::{.panel-tabset}
### female
```{r}
#| layout-ncol: 1
##| fig-height: 4
#| column: page
#| fig-cap: Ribbon plot showing the daily median, minimum and maximum depth of tag 308.
#| - ""
p_308_vertical_speed %>% plotly:: ggplotly () %>%
layout (legend = list (orientation = "h" ,
x = 0.5 , y = - 0.5 ,
xanchor = "center" , yanchor = "top" ))
```
### male
```{r}
#| layout-ncol: 1
##| fig-height: 4
#| column: page
#| fig-cap: Ribbon plot showing the daily median, minimum and maximum depth of tag 321.
#| - ""
p_321_vertical_speed %>% plotly:: ggplotly () %>%
layout (legend = list (orientation = "h" ,
x = 0.5 , y = - 0.5 ,
xanchor = "center" , yanchor = "top" ))
```
:::
:::
# Potential Migration months
:::{.panel-tabset}
### female
:::{.panel-tabset}
#### Sep-Oct
The interesting period is between Sep 20 and October 15.
```{r}
#| layout-ncol: 1
##| fig-height: 4
#| column: page
#| fig-cap: Ribbon plot showing the daily median, minimum and maximum depth of tag 308.
#| - ""
# p_308_vertical_speed %>% plotly::ggplotly() %>%
# layout(legend = list(orientation = "h",
# x = 0.5, y = -0.5,
# xanchor = "center", yanchor = "top"))
```
:::
### male
```{r}
#| layout-ncol: 1
##| fig-height: 4
#| column: page
#| fig-cap: Ribbon plot showing the daily median, minimum and maximum depth of tag 321.
#| - ""
p_321_vertical_speed %>% plotly:: ggplotly () %>%
layout (legend = list (orientation = "h" ,
x = 0.5 , y = - 0.5 ,
xanchor = "center" , yanchor = "top" ))
```
:::
# Horizontal distance
**We need to be careful interpreting this since it's a model output.**
* Overall, the female shark (tag 308) travelled `r tag_308_totalhordistance %>% round(digits = 2)` km and the male shark (tag 321) travelled `r tag_321_totalhordistance %>% round(digits = 2)` km.
* The horizontal distance covered by the female shark (tag 308) is `r total_horizontal_distance %>% filter(tag_serial_number == "1293308") %>% dplyr::select(total_horizontal_distance_km) %>% pull()` km, and for the male shark (tag 321) it is `r total_horizontal_distance %>% filter(tag_serial_number == "1293321") %>% dplyr::select(total_horizontal_distance_km) %>% pull()`
*You can select a zoom window, and select/deselect data by clicking on then on the legend.*
```{r}
#| layout-ncol: 1
##| fig-height: 4
#| column: page
#| fig-cap: Ribbon plot showing the daily median, minimum and maximum depth of tag 321.
#| - ""
p_308_321_hordistance %>% plotly:: ggplotly () %>%
layout (legend = list (orientation = "h" ,
x = 0.5 , y = - 0.5 ,
xanchor = "center" , yanchor = "top" ))
```
* the largest horizontal distance travelled per day is 30 km.
* the overall horizontal distance travelled
<!-- # PCA -->
<!-- # k means clustering -->
# Map
::: {.column-screen-inset}
```{r}
#| include: true
#| layout-ncol: 1
##| fig-height: 4
#| column: page
#| fig-subcap:
#| - ""
# for the long term DST tracks
pal_dst <- colorNumeric (palette = "magma" , domain = c (masterias_dst_geolocation_output$ date_time %>% min (), masterias_dst_geolocation_output$ date_time %>% max ())) # mpts_all$date_time, masterias_detections_clean$date_time
# mpts_short <- mpts_all %>% filter(!tag_serial_number == c("1293308", "1293321"))
legend_dates = c ("2018-08-01" , "2018-11-01" , "2019-02-01" , "2019-05-01" , "2019-08-01" , "2019-11-01" )
base_map %>% hideGroup (c ("stations" , "<span style=color:grey>released tags</span>" , "areas" )) %>%
# add tag 308 ####
addPolylines (data = masterias_dst_geolocation_output %>% filter (tag_serial_number == "1293308" ),
lat = ~ detection_latitude,
lng = ~ detection_longitude,
color = "#B5B5B5" ,
weight = 3 ,
opacity = 0.6 ,
label = ~ tag_serial_number,
group = "tag 308 (female)" ) %>%
addPolylines (data = masterias_dst_geolocation_output %>% filter (tag_serial_number == "1293308" ),
lat = ~ detection_latitude_mean,
lng = ~ detection_longitude_mean,
color = "#B5B5B5" ,
weight = 1 ,
fillOpacity = 0.2 ,
opacity = 0.2 ,
dashArray = "5,5" ,
label = ~ paste0 (tag_serial_number, ", mean track" ),
group = "mean tracks" ) %>%
addPolylines (data = masterias_dst_geolocation_output %>% filter (tag_serial_number == "1293308" ),
lat = ~ detection_latitude_mode,
lng = ~ detection_longitude_mode,
color = "#B5B5B5" ,
weight = 1 ,
fillOpacity = 0.3 ,
dashArray = "1,7" ,
label = ~ paste0 (tag_serial_number, ", mode track" ),
group = "mode tracks" ) %>%
addCircleMarkers (data = masterias_dst_geolocation_output %>% filter (tag_serial_number == "1293308" ),
lat = ~ detection_latitude,
lng = ~ detection_longitude,
fillColor = ~ pal_dst (date_time),
radius = 5 ,
weight = 0 ,
fillOpacity = 0.8 ,
label = ~ paste0 (format (date_time,"%F" ), ", " , sex),
group = "tag 308 (female)" ) %>%
# add tag 321 ####
addPolylines (data = masterias_dst_geolocation_output %>% filter (tag_serial_number == "1293321" ),
lat = ~ detection_latitude,
lng = ~ detection_longitude,
color = "#B5B5B5" ,
weight = 3 ,
opacity = 0.6 ,
label = ~ tag_serial_number,
group = "tag 321 (male)" ) %>%
addPolylines (data = masterias_dst_geolocation_output %>% filter (tag_serial_number == "1293321" ),
lat = ~ detection_latitude_mean,
lng = ~ detection_longitude_mean,
color = "#B5B5B5" ,
weight = 1 ,
fillOpacity = 0.2 ,
opacity = 0.2 ,
dashArray = "5,5" ,
label = ~ paste0 (tag_serial_number, ", mean track" ),
group = "mean tracks" ) %>%
addPolylines (data = masterias_dst_geolocation_output %>% filter (tag_serial_number == "1293321" ),
lat = ~ detection_latitude_mode,
lng = ~ detection_longitude_mode,
color = "#B5B5B5" ,
weight = 1 ,
fillOpacity = 0.3 ,
dashArray = "1,7" ,
label = ~ paste0 (tag_serial_number, ", mode track" ),
group = "mode tracks" ) %>%
addCircleMarkers (data = masterias_dst_geolocation_output %>% filter (tag_serial_number == "1293321" ),
lat = ~ detection_latitude,
lng = ~ detection_longitude,
fillColor = ~ pal_dst (date_time),
radius = 5 ,
weight = 0 ,
fillOpacity = 0.8 ,
label = ~ paste0 (format (date_time,"%F" ), ", " , sex),
group = "tag 321 (male)" ) %>%
# ADD CONTROLS #####
leafem:: addMouseCoordinates () %>%
addLayersControl (position = "topright" ,
baseGroups = c ("EMODnet bathymetry" , "satellite" ),
overlayGroups = c ("tag 308 (female)" , "tag 321 (male)" ,"mean tracks" , "mode tracks" , "wrecks, OWFs, cables" ),
options = layersControlOptions (collapsed = FALSE )) %>%
hideGroup (c ("mean tracks" , "mode tracks" , "wrecks, OWFs, cables" )) %>%
addLegend (position = "topleft" ,
colors = legend_dates %>% as.POSIXct () %>% pal_dst (),
labels = legend_dates, opacity = 1 ,
title = "Dates" )
```
:::
<!-- erstmal bis hier -->
<!-- ## PCA -->
<!-- :::{.panel-tabset} -->
<!-- ### tag 308 -->
<!-- ```{r} -->
<!-- #| layout-ncol: 1 -->
<!-- ##| fig-height: 4 -->
<!-- #| column: page -->
<!-- #| fig-cap: "" -->
<!-- #| - "" -->
<!-- ``` -->
<!-- ### tag 321 -->
<!-- ```{r} -->
<!-- #| layout-ncol: 1 -->
<!-- ##| fig-height: 4 -->
<!-- #| column: page -->
<!-- #| fig-cap: "" -->
<!-- #| - "" -->
<!-- ``` -->
<!-- ::: -->
<!-- ## k-means clustering -->
<!-- :::{.panel-tabset} -->
<!-- * Unsupervised learning: group the first PC axes of the PCA into k clusters. -->
<!-- ### tag 308 -->
<!-- :::{.panel-tabset} -->
<!-- #### daily -->
<!-- ```{r} -->
<!-- #| layout-ncol: 1 -->
<!-- ##| fig-height: 4 -->
<!-- #| column: page -->
<!-- #| fig-cap: "" -->
<!-- #| - "" -->
<!-- plot_f_308_dailydepthrange_cluster %>% plotly::ggplotly() -->
<!-- ``` -->
<!-- #### weekly -->
<!-- #### monthly -->
<!-- ::: -->
<!-- ### tag 321 -->
<!-- ```{r} -->
<!-- #| layout-ncol: 1 -->
<!-- ##| fig-height: 4 -->
<!-- #| column: page -->
<!-- #| fig-cap: "" -->
<!-- #| - "" -->
<!-- ``` -->
<!-- ::: -->
<!-- ## vertical speed -->
<!-- :::{.panel-tabset} -->
<!-- ### tag 308 -->
<!-- ```{r} -->
<!-- #| layout-ncol: 1 -->
<!-- ##| fig-height: 4 -->
<!-- #| column: page -->
<!-- #| fig-cap: "" -->
<!-- #| - "" -->
<!-- ``` -->
<!-- ### tag 321 -->
<!-- ```{r} -->
<!-- #| layout-ncol: 1 -->
<!-- ##| fig-height: 4 -->
<!-- #| column: page -->
<!-- #| fig-cap: "" -->
<!-- #| - "" -->
<!-- ``` -->
<!-- ::: -->
<!-- # horizontal space use -->
<!-- ## horizontal distance per day -->
<!-- **Model outputs need to be taken cautiously!** -->
<!-- :::{.panel-tabset} -->
<!-- ## tag 308 -->
<!-- ```{r} -->
<!-- # weiter machen https://rpubs.com/MarkusLoew/226759 -->
<!-- p <- ggplot() + -->
<!-- geom_line(data = masterias_DVM_sum_day %>% filter(tag_serial_number %in% c("1293308") & horizontal_distance_m < 30000), -->
<!-- aes(x = date_24hcycle, y = horizontal_distance_m)) + -->
<!-- geom_line(data = long_dst_date %>% filter(tag_serial_number %in% c("1293308")), -->
<!-- aes(x = date, y = depth_median_roll3*1000)) + -->
<!-- scale_y_continuous(sec.axis = sec_axis(~./1000, name="median daily depth in m")) + -->
<!-- theme_minimal() + -->
<!-- labs(x = "date", y = "horizontal distance/day in m", colour = "parameter") + -->
<!-- scale_colour_manual(values = c("blue", "red")) -->
<!-- p #%>% plotly::ggplotly() -->
<!-- p2 <- ggplot() + -->
<!-- geom_line(data = masterias_DVM_sum_day %>% filter(tag_serial_number %in% c("1293308") & horizontal_distance_m < 30000), -->
<!-- aes(x = date_24hcycle, y = horizontal_distance_m)) + -->
<!-- geom_line(data = long_dst_date %>% filter(tag_serial_number %in% c("1293308")), -->
<!-- aes(x = date, y = depth_median_roll3, color = "red"), -->
<!-- linewidth = 1) + -->
<!-- scale_y_continuous(name = "horizontal distance/day in m", -->
<!-- sec.axis = sec_axis(~./1000, name="median daily depth in m")) + -->
<!-- theme_minimal() + -->
<!-- labs(x = "date", color = "tag serial number") -->
<!-- p2 %>% plotly::ggplotly() -->
<!-- ``` -->
<!-- ## tag 321 -->
<!-- ```{r} -->
<!-- p <- ggplot() + -->
<!-- geom_line(data = masterias_DVM_sum_day %>% filter(tag_serial_number %in% c("1293321") & horizontal_distance_m < 30000), aes(x = date_24hcycle, y = horizontal_distance_m)) + -->
<!-- theme_minimal() + -->
<!-- labs(x = "date", y = "horizontal distance/day in m", colour = "tag serial number") -->
<!-- p %>% plotly::ggplotly() -->
<!-- ``` -->
<!-- ::: -->